﻿using System;
using System.Collections.Generic;
using Nancy;
using Nancy.ModelBinding;
using WindpowerService.Structures;
using WindpowerService.Structures.Alarm;
using WindpowerService.Structures.ChangePassword;
using WindpowerService.Structures.Experience;
using WindpowerService.Structures.ExperienceAdd;
using WindpowerService.Structures.ExperienceDetail;
using WindpowerService.Structures.Farm;
using WindpowerService.Structures.Generator;
using WindpowerService.Structures.Login;

namespace WindpowerService
{
    public class ServeModule:NancyModule
    {
        public ServeModule()
        {

            Post["/zn_monitoring/ws/WindpowerService/login"] = _ =>
            {
                var postData = this.Bind<LoginRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a Login request with {postData.Yhm} and {postData.Mm}");
                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = new LoginModel
                    {
                        Cfmc = "斗轮机测试服务器",
                        Fhm = "200"
                    }
                });
            };


            Post["/zn_monitoring/ws/WindpowerService/farms"] = _ =>
            {
                var postData = this.Bind<FarmRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a Farm request with {postData.Yhm}");

                var x = Utils.GetRandomInt().ToString();
                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = new FarmModel
                    {
                        Agc = x,
                        Avc = x,
                        Bjs = x,
                        Fs = x,
                        Gzs = x,
                        Klyl = x,
                        Nfdl = x,
                        Pjfs = x,
                        Rfdl = x,
                        Tq = x,
                        Yfdl = x,
                        Zfdl = x,
                        Zgl = x
                    }
                });
            };

            Post["/zn_monitoring/ws/WindpowerService/generatorList"] = _ =>
            {
                var postData = this.Bind<GeneratorListRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a GeneratorList request with {postData.Yhm} amd state {postData.Zt}");
                var generators = new List<GeneratorModel>();

                if (postData.Zt != null)
                {
                    for (int i = 0; i < 20; i++)
                    {
                    var x = Utils.GetRandomInt().ToString();
                        generators.Add(new GeneratorModel
                        {
                            Bh = x,
                            Fs = x,
                            Fx = x,
                            Gl = x,
                            Mc = x,
                            Zt = postData.Zt,
                            Zs = x,
                        });
                    }
                }

                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = generators
                });
            };

            Post["/zn_monitoring/ws/WindpowerService/realFaultList"] = _ =>
            {
                var postData = this.Bind<AlarmRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a Alarm request with {postData.Yhm} ");
                var alarms = new List<AlarmModel>();

               
                    for (int i = 0; i < 20; i++)
                    {
                        var x = Utils.GetRandomInt().ToString();
                        alarms.Add(new AlarmModel
                        {
                            Bh = x,
                            Dm = x,
                            Lx = x,
                            Mc = x,
                            Ms = x,
                            Sj = x
                        });
                    }
                

                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = alarms
                });
            };

            Post["/zn_monitoring/ws/WindpowerService/experienceQuery"] = _ =>
            {
                var postData = this.Bind<ExperienceRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a Experience request with {postData.Yhm} ");
                var exps = new List<ExpericeneModel>();


                for (int i = 0; i < 20; i++)
                {
                    var x = Utils.GetRandomInt().ToString();
                    exps.Add(new ExpericeneModel
                    {
                        Bh = x,
                        Dm = x,
                        Bw = x,
                        Rq = x,
                        Tm = x,
                        Xx = x,
                        Yy= x,
                        Zz = x
                    });
                }


                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = exps
                });
            };


            Post["/zn_monitoring/ws/WindpowerService/experienceQueryId"] = _ =>
            {
                var postData = this.Bind<ExperienceDetailRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a ExperienceDetail request with {postData.Yhm}, No {postData.Bh} ");
                var x = Utils.GetRandomInt().ToString();

                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = new ExperienceDetailModel
                    {
                        Bh = x,
                        Bw = x,
                        Bz = x,
                        Dm = x,
                        Ff = x,
                        Fhm = "200",
                        Rq = x,
                        Tm = x,
                        Xx=x,
                        Yj = x,
                        Yy = x,
                        Zz = x
                    }
                });

            };
            Post["/zn_monitoring/ws/WindpowerService/experienceAdd"] = _ =>
            {
                var postData = this.Bind<ExperienceAddRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a ExperienceAdd request with {postData.Yhm}, {postData.Tm} ");
                var x = Utils.GetRandomInt().ToString();

                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = new ExperienceAddModel
                    {
                        Fhm = "200",
                    }
                });

            };


            Post["/zn_monitoring/ws/WindpowerService/changePassword"] = _ =>
            {
                var postData = this.Bind<ChangePasswordRequest>();
                Console.WriteLine($"{Utils.GetNewId()}_Got a changepassword request with {postData.Yhm} old pwd {postData.Mm} , new pwd {postData.Xmm}");

                return Response.AsJson(new RestResponse
                {
                    Status = 200,
                    Datas = new ChangePasswordModel
                    {
                        Fhm = "200"
                    }
                });
            };

        }
    }
}
